package com.zw.yzk.kabu.data.repository;

import com.zw.yzk.kabu.data.network.task.Task;
import com.zw.yzk.kabu.data.constant.Constant;
import com.zw.yzk.kabu.data.network.ServiceGenerator;
import com.zw.yzk.kabu.data.network.entity.CommentEntity;
import com.zw.yzk.kabu.data.network.service.GetCommentService;
import com.zw.yzk.kabu.data.network.task.GetCommentTask;

import javax.inject.Inject;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by zhanwei on 2017/1/12.
 */

public class CommentListRepository extends CommonRepository {

    @Inject
    public CommentListRepository() {
    }


    @Override
    protected Observable connectServer(Task task) {
        GetCommentService service = ServiceGenerator.createService(GetCommentService.class);
        GetCommentTask commentTask = (GetCommentTask) task;

        if (commentTask.start >= commentTask.total) {
            CommentEntity entity = new CommentEntity();
            //已经全部加载，直接返回空列表
            entity.errCode = Constant.RESULT_OK;
            entity.result.totalComment = commentTask.start;

            return Observable.just(entity);
        } else {
            //加载网络数据
            return service.getCommentList(commentTask.lastId, commentTask.size, commentTask.cosplayId);
        }
    }

    @Override
    protected Func1<CommentEntity, Observable> getServerData() {
        return new Func1<CommentEntity, Observable>() {
            @Override
            public Observable call(CommentEntity entity) {

                return Observable.just(entity);
            }
        };
    }

}
